package com.ksv.qlsv;

import com.ksv.lop.ConnectDB;
import com.ksv.lop.KetQua;
import com.ksv.lop.SinhVien;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;
import android.widget.TextView;

public class SinhVienNhapDiemActivity extends Activity {
	
	public static SinhVien sv = null;
	private Spinner com_hoc_ky;
	private Spinner com_mon_hoc;
	private TextView txt_diem;
	private Button btn_luu;
	private Button btn_huy;
	private KetQua kq = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialogedit_sv_nhapdiem);  
        this.setTitle(sv.getHoTen());
        com_hoc_ky = (Spinner)findViewById(R.id.com_hocky);
        com_mon_hoc = (Spinner)findViewById(R.id.com_monhoc);
        txt_diem = (TextView)findViewById(R.id.txt_diem);
        btn_luu = (Button)findViewById(R.id.btn_luu);
        btn_huy = (Button)findViewById(R.id.btn_huy);
        kq = new KetQua();
        loadListHocKy();
        loadListMonHoc();
        onClickButtonSave();
        onClickButtonCancel();
	}

	private void onClickButtonCancel() {
		btn_huy.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View view) {
				// TODO Auto-generated method stub
				SinhVienNhapDiemActivity.this.finish();
			}
		});
	}

	private void onClickButtonSave() {
		btn_luu.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View view) {
				// TODO Auto-generated method stub
				kq.setDiem(Integer.parseInt(txt_diem.getText().toString()));
				kq.setMaSV(sv.getMaSV());
				ConnectDB.AddKetQua(kq);
				Log.i("TTTH", "count - "+ ConnectDB.getAllKetQua().getCount());
			}
		});
	}

	private void loadListMonHoc() {
		Cursor c = ConnectDB.getAllMonHoc();
		if(c.getCount() >= 1){
			kq.setMaMon((int)com_mon_hoc.getItemIdAtPosition(0));
		}
		startManagingCursor(c);
		SimpleCursorAdapter ca = new SimpleCursorAdapter(this,R.layout.listrow_diem, c, new String [] {ConnectDB.tenmon,"_id"}, new int []{R.id.txt_name});
		com_mon_hoc.setAdapter(ca);
		com_mon_hoc.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View selectedView,
					int position, long id) {
				// TODO Auto-generated method stub
				kq.setMaMon((int)id);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
	}

	private void loadListHocKy() {
		Cursor c = ConnectDB.getAllHocKy();
		if(c.getCount() >= 1){
			kq.setMaHK((int)com_hoc_ky.getItemIdAtPosition(0));
		}
		startManagingCursor(c);
		SimpleCursorAdapter ca = new SimpleCursorAdapter(this,R.layout.listrow_diem, c, new String [] {ConnectDB.tenhk,"_id"}, new int []{R.id.txt_name});
		com_hoc_ky.setAdapter(ca);
		com_hoc_ky.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View selectedView,
					int position, long id) {
				// TODO Auto-generated method stub
				kq.setMaHK((int)id);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	
	
}
